package com.one.rope.mvp.basecore.third.aliyun;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.one.rope.mvp.basecore.util.JsonUtils;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 阿里云短信发送工具类
 * <p>
 *
 * @author ： Weijian.Liu
 * @date ： 2020/3/14 18:45
 */
@Slf4j
public class AliyunSmsUtil {

  private static final String regionId = "cn-hangzhou";

  /**
   * 短信签名【】
   */
  private static final String signName = "多助";

  /**
   * 发送模板短信
   * @param smsSendDTO
   * @return
   */
  public static AliyunSmsSendVO sendTemplateMessage(AliyunSmsSendDTO smsSendDTO) throws Exception{
    DefaultProfile profile = DefaultProfile.getProfile(regionId, smsSendDTO.getAccessKeyId(),
        smsSendDTO.getAccessSecret());
    IAcsClient client = new DefaultAcsClient(profile);

    CommonRequest request = new CommonRequest();
    request.setMethod(MethodType.POST);
    request.setDomain("dysmsapi.aliyuncs.com");
    request.setVersion("2017-05-25");
    request.setAction("SendSms");
    request.putQueryParameter("RegionId", regionId);
    request.putQueryParameter("PhoneNumbers", smsSendDTO.getMobile());
    request.putQueryParameter("SignName", signName);
    request.putQueryParameter("TemplateCode", smsSendDTO.getTemplateCode());
    Map<String, String> code = new HashMap<>();
    code.put("code", smsSendDTO.getContent());
    request.putQueryParameter("TemplateParam", JsonUtils.mapToJsonString(code));
    CommonResponse response = client.getCommonResponse(request);
    log.info("send sms result: [{}]", response.getData());
    AliyunSmsSendVO smsSendVO = (AliyunSmsSendVO)JsonUtils.stringToObject(response.getData(), AliyunSmsSendVO.class);
    return smsSendVO;
  }

}
